/*
 * QLyTVAboutBox.java
 */
package qlytv;

import com.connDB4o.ConnectDB4o;
import com.constants.Constant;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;
import com.datatype.The;
import com.db4o.query.Query;
import javax.swing.JOptionPane;

public class DMThe extends javax.swing.JDialog {
    
    //model default table
    DefaultTableModel model = null;
    
    //flag search: first search    
    String flag = "";

    public DMThe(java.awt.Frame parent) {
        super(parent);
        initComponents();
        getRootPane().setDefaultButton(closeButton);
        txtMaT.setEnabled(false);

        // display textfiled and table
        displayThe();
        
        viewTblThe();
    }

    @Action
    public void closeAboutBox() {
        dispose();
    }

    /**
     * Clear textbox
     */
    public void clearScreen() {
        txtMaT.setText("");
        txtLoaiT.setText("");
        txtGhiChu.setText("");
    }

    /**
     * Clear screen. disable, enable
     */
    @Action
    public void clearThe() {

        txtMaT.setEnabled(true);
        clearScreen();
        //disable and enable
        btnInsert.setEnabled(false);
        btnDelete.setEnabled(false);
        btnSearch.setEnabled(true);
        btnUpdate.setEnabled(false);
        btnSave.setEnabled(true);
    }

    /**
     * display info Phong ban
     */
    public void viewTblThe() {
        //open data
        //set header column
        model = new DefaultTableModel();
        model.addColumn("Mã thẻ");
        model.addColumn("Loại thẻ");
        model.addColumn("Ghi chu");

        ObjectContainer db = ConnectDB4o.connDB4o(Constant.DB4OFILENAME_THE);
        ObjectSet<The> result = db.query(The.class);
        try {
            while (result.hasNext()) {
                The nv = (The) result.next();
                //get values to form
                String rows[] = {nv.getMathe(), nv.getLoaithe(), nv.getGhichu()};
                model.addRow(rows);
                tblThe.setModel(model);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            db.close();
        }

    }

    /**
     * display info NhanVien
     */
    public void displayThe() {
        //open data
        ObjectContainer db = ConnectDB4o.connDB4o(Constant.DB4OFILENAME_THE);
        ObjectSet<The> result = db.query(The.class);
        try {
            while (result.hasNext()) {
                The pb = (The) result.next();
                //get values to form
                txtMaT.setText(pb.getMathe());
                txtLoaiT.setText(pb.getLoaithe());
                txtGhiChu.setText(pb.getGhichu());

                break;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            db.close();
        }

    }

    /**
     * get data on Form
     * @return NhanVien
     */
    public The inputThe() {
        The pb = new The();

        pb.setMathe(txtMaT.getText());
        pb.setLoaithe(txtLoaiT.getText());
        pb.setGhichu(txtGhiChu.getText());

        return pb;
    }

    /**
     * insert The
     */
    @Action
    public void insThe() {

        if (txtMaT.getText().isEmpty()) {
            //message dialog
            JOptionPane.showMessageDialog(null, "Mã thẻ không được trống.", "Nhập thông tin", JOptionPane.INFORMATION_MESSAGE);
            //set focus
            txtMaT.requestFocus();
        } else {
            The the = inputThe();
            String flag = ConnectDB4o.insertData(Constant.DB4OFILENAME_THE, null, null, the, null);

            //disable and enable
            btnInsert.setEnabled(true);
            btnDelete.setEnabled(true);
            btnSearch.setEnabled(true);
            btnUpdate.setEnabled(true);
            btnSave.setEnabled(false);
            txtMaT.setEnabled(false);
            if (flag.isEmpty()) {
                btnSave.setEnabled(true);
                txtMaT.setEnabled(true);
            }
            //Hien thi bang The len luoi
            viewTblThe();
        }

    }

    /**
     * update The
     */
    @Action
    public void updThe() {

        The the = inputThe();
        ConnectDB4o.updateData(Constant.DB4OFILENAME_THE, null, null, the, null);

        //disable and enable
        btnInsert.setEnabled(true);
        btnDelete.setEnabled(true);
        btnSearch.setEnabled(true);
        btnUpdate.setEnabled(true);
        btnSave.setEnabled(false);

        //Hien thi bang The len luoi
        viewTblThe();

    }

    /**
     * Delete The
     */
    @Action
    public void delThe() {     
        Boolean check = ConnectDB4o.chkData(Constant.DB4OFILENAME_DOCGIA, txtMaT.getText());
        if (check.booleanValue()) {
            QLyTVView.showMessage("Mã thẻ:" + txtMaT.getText() + " đã được cho mượn");
        }
        else{
        
        ConnectDB4o.delData(Constant.DB4OFILENAME_THE, txtMaT.getText());
        
        //disable and enable
        btnInsert.setEnabled(true);
        btnDelete.setEnabled(true);
        btnSearch.setEnabled(true);
        btnUpdate.setEnabled(true);
        btnSave.setEnabled(false);

        //Hien thi bang The len luoi
        viewTblThe();
        }
    }

    /**
     * search The
     */
    @Action
    public void findThe() {        
        
        if (flag.isEmpty()) {
            //clear screen
            clearThe();
            //set flag
            flag = "X";
        } 
        else {
            //find this NhanVien
            The nv_ = inputThe();
            //set header column
            model = new DefaultTableModel();
            model.addColumn("Mã thẻ");
            model.addColumn("Loại thẻ");
            model.addColumn("Ghi chu");

            //open data
            ObjectContainer db = ConnectDB4o.connDB4o(Constant.DB4OFILENAME_THE);
            ObjectSet result = null;
            try {
                Query query = db.query();
                query.constrain(The.class);

                if (txtMaT.getText().isEmpty()) {
                    query.descend("Mathe").constrain("").like();
                } else {
                    query.descend("Mathe").constrain(nv_.getMathe()).like();
                }

                if (txtLoaiT.getText().isEmpty()) {
                    query.descend("Loaithe").constrain("").like();
                } else {
                    query.descend("Loaithe").constrain(nv_.getLoaithe()).like();
                }


                if (txtGhiChu.getText().isEmpty()) {
                    query.descend("Ghichu").constrain("").like();
                } else {
                    query.descend("Ghichu").constrain(nv_.getGhichu()).like();
                }

                result = query.execute();

                if (result.size() > 0) {
                    while (result.hasNext()) {
                        The nv = (The) result.next();
                        //get values to form
                        String rows[] = {nv.getMathe(), nv.getLoaithe(), nv.getGhichu()};
                        model.addRow(rows);
                        tblThe.setModel(model);
                    }
                } else {
                    String rows[] = {"", "", ""};
                    model.addRow(rows);
                    tblThe.setModel(model);
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                db.close();
            }

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblThe = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();
        btnInsert = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtGhiChu = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtMaT = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtLoaiT = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(qlytv.QLyTVApp.class).getContext().getResourceMap(DMThe.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setModal(true);
        setName("aboutBox"); // NOI18N
        setResizable(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblThe.setName("tblThe"); // NOI18N
        tblThe.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblTheMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblThe);

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(qlytv.QLyTVApp.class).getContext().getActionMap(DMThe.class, this);
        closeButton.setAction(actionMap.get("closeAboutBox")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        btnInsert.setAction(actionMap.get("clearThe")); // NOI18N
        btnInsert.setText(resourceMap.getString("btnInsert.text")); // NOI18N
        btnInsert.setName("btnInsert"); // NOI18N

        btnUpdate.setAction(actionMap.get("updThe")); // NOI18N
        btnUpdate.setText(resourceMap.getString("btnUpdate.text")); // NOI18N
        btnUpdate.setName("btnUpdate"); // NOI18N

        btnDelete.setAction(actionMap.get("delThe")); // NOI18N
        btnDelete.setText(resourceMap.getString("btnDelete.text")); // NOI18N
        btnDelete.setName("btnDelete"); // NOI18N

        btnSearch.setAction(actionMap.get("findThe")); // NOI18N
        btnSearch.setText(resourceMap.getString("btnSearch.text")); // NOI18N
        btnSearch.setName("btnSearch"); // NOI18N

        btnSave.setAction(actionMap.get("insThe")); // NOI18N
        btnSave.setText(resourceMap.getString("btnSave.text")); // NOI18N
        btnSave.setName("btnSave"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnInsert, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(closeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(btnInsert)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSearch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeButton))
        );

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtGhiChu.setText(resourceMap.getString("txtGhiChu.text")); // NOI18N
        txtGhiChu.setName("txtGhiChu"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtMaT.setText(resourceMap.getString("txtMaT.text")); // NOI18N
        txtMaT.setName("txtMaT"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtLoaiT.setText(resourceMap.getString("txtLoaiT.text")); // NOI18N
        txtLoaiT.setName("txtLoaiT"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtGhiChu, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                    .addComponent(txtLoaiT, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                    .addComponent(txtMaT, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
                .addGap(24, 24, 24))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMaT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtLoaiT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtGhiChu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(80, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 512, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblTheMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTheMouseClicked
        int rows = tblThe.getSelectedRow();
        //set text with select rows
        txtMaT.setText((String) tblThe.getModel().getValueAt(rows, 0));
        txtLoaiT.setText((String) tblThe.getModel().getValueAt(rows, 1));
        txtGhiChu.setText((String) tblThe.getModel().getValueAt(rows, 2));
    }//GEN-LAST:event_tblTheMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblThe;
    private javax.swing.JTextField txtGhiChu;
    private javax.swing.JTextField txtLoaiT;
    private javax.swing.JTextField txtMaT;
    // End of variables declaration//GEN-END:variables
}
